<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Member extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model('Member_model');
		$this->load->library('session');
		$this->load->helper('url');
		$this->load->helper('bantuan_helper');
		if(!$this->session->userdata('user_id') && !$this->session->userdata('user_display_name') ):
			redirect('backend');
		endif;
	}
	

	function index()
	{
		$this->load->library('pagination');
		$offset = $this->uri->segment(4);
		$config['base_url'] = site_url().'/backend/member/index';
		$config['total_rows'] = count($this->Member_model->getMember('all',FALSE,FALSE,FALSE));
		$config['per_page'] = $this->config->item('per_page');
		$config['uri_segment'] = '4';
		
		$data['urut'] = $this->uri->segment(4);
		$data['title'] = 'Member';
		$data['template'] = 'member/index';
		$data['res'] = $this->Member_model->getMember('all',FALSE,$config['per_page'],$offset);
		$this->pagination->initialize($config);
		$this->load->view('backend/index',$data);
	}
	
	function search_result()
	{
		$this->form_validation->set_rules('nama', 'Nama', 'required');
		
		if ($this->form_validation->run() == FALSE)
		{	
			redirect('backend/member');
		}
		else
		{
			$this->load->library('pagination');
			$offset = $this->uri->segment(4);
			$config['base_url'] = site_url().'/backend/member/index';
			$config['total_rows'] = count($this->Member_model->getMember('cari',FALSE,FALSE,FALSE,$this->input->post('nama')));
			$config['per_page'] = $this->config->item('per_page');
			$config['uri_segment'] = '4';
			
			$data['urut'] = $this->uri->segment(4);
			$data['title'] = 'Hasil Cari';
			$data['template'] = 'member/index';
			$data['res'] = $this->Member_model->getMember('cari',FALSE,$config['per_page'],$offset,$this->input->post('nama'));
			$this->pagination->initialize($config);
			$this->load->view('backend/index',$data);
		}
	}
	
	function edit()
	{
		$this->form_validation->set_rules('nama', 'Nama', 'required');
		$this->form_validation->set_rules('jenkel', 'Jenis Kelamin', 'required');
		$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
		$this->form_validation->set_rules('telp', 'Telepon', 'required');
		$this->form_validation->set_rules('hp', 'Mobile Phone / HP', 'required');
		$this->form_validation->set_rules('alamat', 'Alamat', 'required');
		$this->form_validation->set_rules('prov_id', 'Propinsi', 'required');
		$this->form_validation->set_rules('kab_id', 'Kabupaten', 'required');
		$this->form_validation->set_rules('kota', 'Kota', 'required');
		$this->form_validation->set_rules('kodepos', 'Kode Pos', 'required');
		
		if ($this->form_validation->run() == FALSE)
		{
			$id = $this->uri->segment(4);
			$data['title'] = 'Member';
			$data['template'] = 'member/edit';
			$data['row'] = $this->Member_model->getMember(FALSE,$id,FALSE,FALSE);
			$this->load->view('backend/index',$data);
		}
		else
		{
			if ($this->input->post('password') != '')
			{
				$this->form_validation->set_rules('password', 'Password', 'required|matches[retype_password]');
				$this->form_validation->set_rules('retype_password', 'Password Confirmation', 'required');
				if ($this->form_validation->run() == FALSE)
				{
					$id = $this->uri->segment(4);
					$data['title'] = 'Member';
					$data['template'] = 'member/edit';
					$data['row'] = $this->Member_model->getMember(FALSE,$id,FALSE,FALSE);
					$this->load->view('backend/index',$data);
				}
				else
				{
					$id = $this->uri->segment(4);
					$uri = $this->uri->segment(5);
					$data = array('nama' => $this->input->post('nama'),
									  'jenkel' => $this->input->post('jenkel'),
									  'email' => $this->input->post('email'),
									  'password' => md5($this->input->post('password')),
									  'telp' => $this->input->post('telp'),
									  'hp' => $this->input->post('hp'),
									  'alamat' => $this->input->post('alamat'),
									  'mem_prov_id' => $this->input->post('prov_id'),
									  'mem_kab_id' => $this->input->post('kab_id'),
									  'kota' => $this->input->post('kota'),
									  'kodepos' => $this->input->post('kodepos'),
									  'date' => date('Y-m-d'),
									  'status'=> $this->input->post('stat_mem')
									  );  
					$this->Member_model->editMember($id,$data);
					redirect('backend/member');
				}
			}
			else
			{
				$id = $this->uri->segment(4);
				$uri = $this->uri->segment(5);
				$data = array('nama' => $this->input->post('nama'),
									  'jenkel' => $this->input->post('jenkel'),
									  'email' => $this->input->post('email'),
									  'telp' => $this->input->post('telp'),
									  'hp' => $this->input->post('hp'),
									  'alamat' => $this->input->post('alamat'),
									  'mem_prov_id' => $this->input->post('prov_id'),
									  'mem_kab_id' => $this->input->post('kab_id'),
									  'kota' => $this->input->post('kota'),
									  'kodepos' => $this->input->post('kodepos'),
									  'date' => date('Y-m-d'),
									  'status'=> $this->input->post('stat_mem')
									  );  
				$this->Member_model->editMember($id,$data);
				redirect('backend/member');
			}
		}
	}
	
	function edit_block()
	{
		$id = $this->uri->segment(4);
		$uri = $this->uri->segment(5);
		$data = array('status'=>'0');
		$this->Member_model->editMember($id,$data);
		redirect('backend/member');
	}
	
	function edit_aktif()
	{
		$id = $this->uri->segment(4);
		$uri = $this->uri->segment(5);
		$data = array('status'=>'1');
		$this->Member_model->editMember($id,$data);
		redirect('backend/member');
	}
	
	function ajax_bulk_action($param)
	{
		foreach($_POST["member_id"] as $id):
			if($param=='block'):
				$data = array('status'=>0);
				$this->db->where('member_id',$id)->update('tb_member',$data);
			elseif($param=='unblock'):
				$data = array('status'=>1);
				$this->db->where('member_id',$id)->update('tb_member',$data);
			elseif($param=='delete'):
			$this->db->where('member_id',$id);
			$this->db->delete('tb_member');
			endif;
		endforeach;
	}
	
	
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/backend/customer.php */
